from flask import current_app
from flask import g
from flask import session

from info.modules import User


def index_class(index):
    if index == 1:
        return 'first'
    elif index == 2:
        return 'second'
    elif index == 3:
        return 'third'
    else:
        return ''


import functools
# 自定义装饰器，检测用户的登陆状态
def login_required(f):
    @functools.wraps(f)
    def wrapper(*args,**kwargs):
        user_id = session.get('user_id')
        user = None
        # 判断user_id
        if user_id:
            try:
                user = User.query.filter_by(id = user_id).first()
            except Exception as e:
                current_app.logger.error(e)
        # 使用g对象存储user信息
        g.user = user
        return f(*args,**kwargs)
    return wrapper